<?php
/*****************************************************************
 * Fusion.Board
 *
 * @author                Denis Wrobel
 * @license               http://www.gnu.org/licenses/gpl-3.0.html
 * @version               0.1
 *
 *****************************************************************/

if( !defined( 'IN_SCRIPT' ) )
{
    die( 'Go Away!' );
}

class Page
{
    public $breadcrumb;
    public $title;
    
    public function __construct()
    {
        global $language, $board;
        
        $this->breadcrumb = '<img src="./templates/default/icons/house.png" /> <a href="index.php">' . htmlspecialchars( $board['board_title'] ) . ' - ' . $language->string( 'index_page' ) . '</a> ';
    }
    
    public function addBreadcrumb( $target, $title )
    {
        $this->breadcrumb .= '<img src="./templates/default/icons/bullet_go.png" /> <a href="' . $target . '">' . htmlspecialchars( $title ) . '</a> ';
    }
    
    public function drawHeader()
    {
        global $template, $session, $language, $board, $core;
        
        if( $board['enable_announcement'] )
        {
            $template->setIf( 'announcement' );
        }
        
        $template->assignVars( array('title'        => ( isset( $this->title ) ) ? htmlspecialchars( $this->title ) . ' - Fusion Board' : htmlspecialchars( $board['board_title'] ) . ' - Fusion Board', 
                                     'l_index_page' => $language->string( 'index_page' ),
                                     'l_users_list' => $language->string( 'users_list' ),
                                     'a_users_list' => 'index.php?action=members',
                                     'breadcrumbs'  => $this->breadcrumb,
                                     'l_announcement'=>$language->string( 'announcement' ),
                                     'announcement' => $board['announcement_message'],
                                     'l_loading'    => $language->string( 'loading' ) ) );

        if( !$session['session_logged'] )
        {
            $template->setIf( 'is_guest' );
            $guestBlock = $template->assignBlock( 'welcomeGuest', array( 'l_welcome_guest' => $language->string( 'welcome_guest' ),
                                                                         'a_register'      => 'index.php?action=register',
                                                                         'l_register'      => $language->string( 'register' ),
                                                                         'a_log_in'        => 'index.php?action=login',
                                                                         'l_log_in'        => $language->string( 'log_in' ) ) );
    
            $template->assignVar( 'welcomeBlock', $guestBlock );
        }
        else
        {
            $template->setIf( 'is_user' );
            $userBlock = $template->assignBlock( 'welcomeUser', array(  'l_welcome'    => $language->string( 'welcome_user' ),
                                                                        'username'     => $core->htmlFilter( $session['username'] ),
                                                                        'a_profile'    => 'index.php?action=usercp&mode=view&u=' . $session['user_id'], 
                                                                        'l_user_panel' => $language->string( 'your_panel' ),
                                                                        'a_user_panel' => 'index.php?action=usercp',
                                                                        'l_logout'     => $language->string( 'logout' ),
                                                                        'a_logout'     => 'index.php?action=usercp&mode=logout' ) );
    
            $template->assignVar( 'welcomeBlock', $userBlock );
        }
        
        $template->draw( 'header', 1 );
    }
    
    public function drawFooter()
    {
        global $template, $session, $language, $generation_time, $db;
        
        $template->assignVars( array( 'queries'   => $db->queries_num,
                                      'gen_time'  => substr( microtime( true ) - $generation_time, 0, 6 ),
                                      'sql_usage' => round( $db->quering_time / (microtime( true ) - $generation_time) * 100 ),
                                      'php_usage' => 100 - round( $db->quering_time / (microtime( true ) - $generation_time) * 100 ),
                                      'mem_usage' => memory_get_usage( true ) / 1024 / 1024 ) );
        
        $template->draw( 'footer', 1 );
    }
    
    public function error( $error_msg )
    {
        global $template, $language;
        
        $template->assignVars( array( 'l_error_occured' => $language->string( 'error_occured' ),
                                      'l_error_message' => $language->string( 'error_message' ),
                                      'message'         => $error_msg ) );
        
        $this->drawHeader();
        $template->draw( 'error_page' );
        $this->drawFooter();
        
        die();
    }
    
    public function redirect( $target, $message )
    {
        global $language, $template;
        
        $template->assignVars( array( 'target'        => $target,
                                      'l_redirection' => $language->string( 'redirection' ),
                                      'message'       => $message,
                                      'l_go_forward'  => $language->string( 'go_forward' ) ) );
        
        $template->draw( 'redirect' );
        die();
    }
}

?>